<?php 

$auth = $_SESSION['auth'];
if (empty($auth)) die('用户未登录！^_^');

if (!empty($_POST)) die;

$topic_id = intval($_GET['topic_id']);
$page = intval($_GET['page']);

if (!dt_query_one("SELECT user_id FROM forum_topic WHERE id = '$topic_id' AND user_id = '".$auth['id']."'")) die("错误的操作！^_^");   //检查订单所属

if (empty($page)) $page = 1; 
$limit = require_once('inc/order_search_limit.php');

$orders = dt_query("SELECT id, complete FROM forum_topic_order WHERE topic_id = '$topic_id' ORDER BY complete, c_at DESC LIMIT ".$limit * ($page - 1).",$limit");
if (!$orders) die('获取forum_topic_orders数据失败！^_^');

//全部标记已经发货并变更订单量提示
$c_o_ids = '';
$order_c_chg = 0;
while($order = mysql_fetch_array($orders)) {
	if (0 == $order['complete']) {
		$c_o_ids .= empty($c_o_ids) ? $order['id'] : ', '.$order['id'];
		$order_c_chg++;
	}
}
if (0 < $order_c_chg) {
	$rs = dt_query("UPDATE forum_topic_order SET complete = 1 WHERE id IN ($c_o_ids)");
	if (!$rs) die('更新forum_topic_order数据失败！^_^'); 
	$rs = dt_query("UPDATE forum_topic SET order_c = order_c - $order_c_chg WHERE id = $topic_id");
	if (!$rs) die('变更订单量order_c提示失败！^_^');
}

die('s0');
